
/// by Alvaro Cuno, LCG-COPPE-UFRJ
/// bugs? email-me: alvaroecp@gmail.com
/// Feb, 2007

#ifndef __MESH_H__
#define __MESH_H__

#include <vector>
#include <list>
#include <set>

#include "../typedefs.h"

using namespace std;
   
class Mesh {
   
public:
   Mesh();
   Mesh(const char* filename);
   ~Mesh();
   
   unsigned nvertices() const { return _nvertices; }
   unsigned nfaces()    const { return _nfaces; }
   
   void loadShape(const char *filename);
   void saveShapeOFF(const char *filename);
   
   /// Reads an off model into a polyhedron data structure and returns it
   /// @param filename Name of the file containing model
   /// @return pointer to newly read model or 0 if unsuccessful
   static Mesh* load_off (const char *filename);
   
protected:
   
   /// Computes auxiliar data 
   void auxiliarData();
   
   /// This function sets the minimum and maximum points of the model
   /// These two points will edited to be represent a cube
   void limits();
   
   void genVerticesNormals();
   
   void genFacesNormals();
   
   /// This function centers the model at the origin and
   /// rescales the vertex positions by a scale factor 
   /// @param factor the uniform scale factor. 
   /// The value 2 means a scale between [(-1,-1,-1) (1,1,1)]
   void centerAndScale(Number factor=2.0);
   
public:
   vector<Point3>       positions;     /// vertex positions array
   vector<Vector3>      normals;       /// vertex normals array
   vector<unsigned>     faces;         /// faces array
   vector<Vector3>      facenormals;   /// face normal array
   vector<set<int> >    facespervertex;/// stores for each vertex the set of incident faces indices
   Number               mincoo[3];     /// minimum point coordinates
   Number               maxcoo[3];     /// maximum point coordinates
   
protected:
   unsigned             _nvertices;    /// nro of vertices
   unsigned             _nfaces;       /// nro of faces
};

#endif
